import Vue from 'vue';
import VueRouter from 'vue-router';
import axios from 'axios';
import Vuex from 'vuex';
import App from '@/App';
import Toast from '@/common/vue-toast';
import "@/assets/css/toast.css";

import routes from '@/router';
import { state, mutations, getters, actions } from '@/vuex/baseApi';
import openApi from '@/vuex/openApi';
import userApi from '@/vuex/userApi';
import accountApi from '@/vuex/accountApi';


// 注册组件
Vue.use(VueRouter);
Vue.use(Vuex);
Vue.use(Toast);


//组件配置
const router = new VueRouter({
  routes
});// 导出一个新生成的router对象
const store = new Vuex.Store({
  state, mutations, getters, actions,
  modules: {
    openApi, userApi, accountApi
  }
});// 导出一个新生成的Store对象
// 在vue原型中添加$http方法等于axios
//axios.defaults.baseURL = 'http://atest.91tou.com';
//axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
//axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
Vue.prototype.$http = axios;
Vue.config.productionTip = false;


//路由拦截
router.beforeEach(({meta, path}, from, next) => {
  var { auth = true } = meta;
  var isLogin = Boolean(store.state.userApi.isLogin);
  if (auth && !isLogin && path !== '/login') {
    return next({path: '/login'})
  }
  next()
});


/* 创建和挂载根实例。通过 router 配置参数注入路由，从而让整个应用都有路由功能 */
const app = new Vue({
  router, store,
  render: h=>h(App)
}).$mount('#app');
